package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("菜品分类管理")
@Slf4j
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation("新增菜品分类")
    @PostMapping
    public Result<String> addCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增菜品分类 categoryDTO={}", categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 获取菜品分类列表
     * @param categoryPageQueryDTO
     * @return
     */
    @ApiOperation("获取菜品分类列表")
    @GetMapping("/page")
    public Result<PageResult> getCategoryList(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("获取菜品分类列表 categoryPageQueryDTO={}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.getCategoryList(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据类型查询菜品分类
     * @param type
     * @return
     */
    @ApiOperation("根据类型查询菜品分类")
    @GetMapping("/list")
    public Result<List<Category>> searchCategory(Integer type) {
        log.info("根据类型查询菜品分类: type={}", type);
        List<Category> categoryList = categoryService.searchCategory(type);
        return Result.success(categoryList);
    }

    /**
     * 设置菜品分类状态
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("设置菜品分类状态")
    @PostMapping("/status/{status}")
    public Result<String> setCategoryStatus(Long id, @PathVariable Integer status) {
        log.info("设置菜品分类状态: id={}, status={}", id, status);
        categoryService.setCategoryStatus(id, status);
        return Result.success();
    }

    @ApiOperation("根据id删除菜品分类")
    @DeleteMapping
    public Result<String> deleteCategoryCategoryById(Long id) {
        log.info("根据id删除菜品分类: id={}", id);
        categoryService.deleteCategoryById(id);
        return Result.success();
    }

    @ApiOperation("更新菜品分类")
    @PutMapping
    public Result<String> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        log.info("更新菜品分类: categoryDTO={}", categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

}
